package com.itheima.category.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.category.mapper.CategoryMapper;
import com.itheima.goods.pojo.Category;
import com.itheima.category.service.CategoryService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;
    @Override
    public Result findAll() {

        List<Category> list=this.list();
        return new Result(true, StatusCode.OK,null, list);
    }

    @Override
    public Result add( Category category) {
//        LambdaUpdateWrapper<Category> wrapper = Wrappers.lambdaUpdate();
//        wrapper.eq(Category::getId,category.getId())
//                .eq(Category::getIsMenu,category.getIsMenu());
//        categoryMapper.update(category,wrapper);
        int i = baseMapper.insert(category);
        if(i>0){
           return new Result(true,StatusCode.OK,"添加成功",null);
        }
        return new Result(false,StatusCode.ERROR,"添加失败",null);
    }

    @Override
    public Result findPage(Integer page, Integer size) {
        Page<Category> pages = new Page<>(page,size);
        Page<Category> categoryPage = baseMapper.selectPage(pages,null);
        PageResult<Category> categoryPageResult = new PageResult<>();
        categoryPageResult.setRows(pages.getRecords());
        categoryPageResult.setTotal(categoryPage.getTotal());
        return new Result(true,StatusCode.OK,null,categoryPageResult);
    }

    @Override
    public Result findById(Integer id) {
        LambdaQueryWrapper<Category> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(Category::getId,id);
        Category category = categoryMapper.selectOne(wrapper);
        return new Result(true,StatusCode.OK,null,category);
    }

    @Override
    public Result update(Integer id,Category category) {
       // LambdaQueryWrapper<Category> wrapper = Wrappers.lambdaQuery();
        LambdaUpdateWrapper<Category> wrapper = Wrappers.lambdaUpdate();
        wrapper.eq(Category::getId,id)
                .set(Category::getIsMenu,category.getIsMenu())
                .set(Category::getIsShow,category.getIsShow())
                .set(Category::getName,category.getName())
                .set(Category::getParentId,category.getParentId())
                .set(Category::getSeq,category.getSeq());
        int i = baseMapper.update(category, wrapper);
        if(i>0){
            return new Result(true, StatusCode.OK,"修该成功",null);
        }
        return new Result(false, StatusCode.ERROR,"未修该成功",null);
    }

    @Override
    public Result delete(Integer id) {
        int i = baseMapper.deleteById(id);
        if(i>0){
            return  new Result(true, StatusCode.OK,"删除成功",null);
        }
        return  new Result(false, StatusCode.ERROR,"未删除成功",null);

    }


}
